package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ProjectSourseMapper;
import com.hbnu.system.model.entity.ProjectSourse;
import com.hbnu.system.service.ProjectSourseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ProjectSourseServiceImpl extends ServiceImpl<ProjectSourseMapper, ProjectSourse> implements ProjectSourseService {
    @Override
    public boolean addProjectSourse(ProjectSourse projectSourse) {
        ProjectSourse projectSourse1 = super.getOne(Wrappers.lambdaQuery(ProjectSourse.class)
                .eq(ProjectSourse::getProjectSourseName,projectSourse.getProjectSourseName()));
        if(StrUtil.isEmpty(projectSourse.getProjectSourseName())||StrUtil.isBlank(projectSourse.getProjectSourseName())){
            throw new BaseException("该项目来源名称为空");
        }
        if(!Objects.isNull(projectSourse1)){
            throw new BaseException("该项目来源名称已存在");
        }
        return baseMapper.insert(projectSourse)==1;
    }

    @Override
    public List<ProjectSourse> getProjectSourseList() {
        List<ProjectSourse> depts = baseMapper.selectList(Wrappers.lambdaQuery(ProjectSourse.class));
//        List<String> result = new ArrayList<>();
//        for (Dept dept : depts) {
//            result.add(dept.getDeptName());
//        }
        return depts;
    }

    @Override
    public boolean updateProjectSourse(ProjectSourse projectcate) {
        if(StrUtil.isEmpty(projectcate.getProjectSourseName())||StrUtil.isBlank(projectcate.getProjectSourseName())){
            throw new BaseException("该项目来源名称为空");
        }
        if(baseMapper.updateById(projectcate)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteProjectSourse(String ID) {
        return baseMapper.deleteById(ID) == 1;
    }

    @Override
    public List<ProjectSourse> getByProjectSourseName(String projectSourseName) {
        LambdaQueryWrapper<ProjectSourse> queryWrapper = Wrappers.lambdaQuery(ProjectSourse.class);

        // 如果 projectSourseName 不为空且不是空白字符串，则添加模糊匹配条件
        if (StringUtils.isNotBlank(projectSourseName)) {
            queryWrapper.like(ProjectSourse::getProjectSourseName, projectSourseName);
        }

        return baseMapper.selectList(queryWrapper);
    }
}
